package com.topofstack.flp.ui;

import com.bappi.custom.activities.CustomActivity;
import com.topofstack.flp.FLPApplication;
import com.topofstack.flp.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SettingsActivity extends CustomActivity implements OnClickListener {
	Button btnSave;
	EditText txtRetryPreiod;
	EditText txtMaxLoginAttempts;
	EditText txtLockoutPeriod;
	EditText txtPass1;
	EditText txtPass2;
	FLPApplication application;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_settings);
		
		application = (FLPApplication)getApplication();
		
		btnSave = (Button)findViewById(R.id.btnSave);
		btnSave.setOnClickListener(this);
		
		txtRetryPreiod = (EditText)findViewById(R.id.txtRetryPreiod);
		txtMaxLoginAttempts = (EditText)findViewById(R.id.txtMaxLoginAttempts);
		txtLockoutPeriod = (EditText)findViewById(R.id.txtLockoutPeriod);
		txtPass1 = (EditText)findViewById(R.id.txtPass1);
		txtPass2 = (EditText)findViewById(R.id.txtPass2);
		
		
		int retryPeriod = application.getPreferenceManager().config_getInterval();
		txtRetryPreiod.setText(""+retryPeriod);
		
		int maxLoginAttempts = application.getPreferenceManager().config_getChance();
		txtMaxLoginAttempts.setText(""+maxLoginAttempts);
		
		int lockoutPeriod = application.getPreferenceManager().config_getLock();
		txtLockoutPeriod.setText(""+lockoutPeriod);
		
	}

	public int isNumber(String str)  
	{  
		int d;
		try  
		{  
			d = Integer.parseInt(str);  
		}  
		catch(NumberFormatException nfe)  
		{  
			return -1;  
		}  
		return d;  
	}
	
	
	@Override
	public void onClick(View sender) {
		if(sender == btnSave){
			
			int retryPeriod = isNumber(txtRetryPreiod.getText().toString());
			int maxLoginAttempts = isNumber(txtMaxLoginAttempts.getText().toString());
			int lockoutPeriod = isNumber(txtLockoutPeriod.getText().toString());
			
			if(retryPeriod < 0 || retryPeriod < 0 || lockoutPeriod < 0){
				FLPApplication.showAlert(getParent(), 
						"Please enter valid positive numbers.", 
						"Oops!");
				return;
			}
			
			String pass1 = txtPass1.getText().toString();
			String pass2 = txtPass2.getText().toString();
			
			if("".equals(pass1) == false || "".equals(pass2) == false){
				if(pass1.equals(pass2)){
					application.getPreferenceManager().setPass(pass1);
				}
				else{
					FLPApplication.showAlert(getParent(), 
							"Two passwords does not match", 
							"Oops!");
					return;
				}
			}
			
			application.getPreferenceManager().config_setChance(maxLoginAttempts);
			application.getPreferenceManager().config_setInterval(retryPeriod);
			application.getPreferenceManager().config_setLock(lockoutPeriod);
			
			FLPApplication.showAlert(getParent(), "Saved successfully", "");
			
		}
	}
	
	
}
